package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IPatrolMaintainHandleView;
import com.cloudsigns.factorymanager2.engineering.adapter.GridAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.MaintainForPatrolBean;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;
import com.cloudsigns.factorymanager2.engineering.bean.PatrolFeedbackBean;
import com.cloudsigns.factorymanager2.engineering.bean.PatrolOrderBean;
import com.cloudsigns.factorymanager2.engineering.presenter.PatrolFeedBackSurePresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.PatrolMaintainHandlePresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.PatrolOrderDetailPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2018/7/22.
 */

public class PatrolExceptionHandleActivity extends Activity implements View.OnClickListener, IPatrolMaintainHandleView {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView edt_engineering_name, edt_number, edt_maintain_type, edt_maintain_content;
    private View rl_handle_date;
    private TextView edt_handle_date;
    private TextView btn_submit;

    private PatrolOrderBean patrolOrderBean;
    private MaintainForPatrolBean maintainForPatrolBean;
    private MediaBean mediaBean;
    private String tag;
    /*订单id,灯箱id,维修项id*/
    private String orderId, itemId, handleId;
    private boolean isClick;
    private PatrolMaintainHandlePresenter patrolMaintainHandlePresenter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_patrol_exception_hadle);

        patrolMaintainHandlePresenter = new PatrolMaintainHandlePresenter(this);
        init();

        Intent mIntent = getIntent();
        if (null != mIntent) {
            patrolOrderBean = (PatrolOrderBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);
            maintainForPatrolBean = (MaintainForPatrolBean) mIntent.getSerializableExtra("maintainForPatrolBean");
            mediaBean = (MediaBean) mIntent.getSerializableExtra("mediaBean");

            if(null != mediaBean){
                itemId = mediaBean.getId();
                edt_number.setText(mediaBean.getMediaName() + "/" + mediaBean.getNumber());
            }

            if (null != patrolOrderBean) {
                orderId = patrolOrderBean.getId();
//                List<MediaBean> mediaBeanList = patrolOrderBean.getMediaList();
//                if (null != mediaBeanList && mediaBeanList.size() > 0) {
//                    MediaBean mediaBean = mediaBeanList.get(0);
//                    itemId = mediaBean.getId();
//                    edt_number.setText(mediaBean.getMediaName() + "/" + mediaBean.getNumber());
//                }

                edt_engineering_name.setText(patrolOrderBean.getRegionName());

            }

            if (null != maintainForPatrolBean) {
                handleId = maintainForPatrolBean.getHandleId();
                boolean isHandle = maintainForPatrolBean.isHandle();
                if (isHandle) {
                    rl_handle_date.setVisibility(View.VISIBLE);
                    if (null != patrolOrderBean) {
                        long handleDate = patrolOrderBean.getPatrolFeedbackList().get(0).getHandleDate();
                        edt_handle_date.setText(DateUtil.getYyyy_Mm_Dd(handleDate));
                    }

                    btnClickable(false);
                } else {
                    rl_handle_date.setVisibility(View.GONE);
                    btnClickable(true);
                }


                CommonEnum.MaintainList patrolFeedbackItemEnum = maintainForPatrolBean.getPatrolFeedbackItemEnum();
                if (null != patrolFeedbackItemEnum) {
                    if (patrolFeedbackItemEnum == CommonEnum.MaintainList.useEnvironment) {
                        edt_maintain_type.setText("使用环境");
                    } else if (patrolFeedbackItemEnum == CommonEnum.MaintainList.waterProofEnvironment) {
                        edt_maintain_type.setText("防水环境");
                    } else if (patrolFeedbackItemEnum == CommonEnum.MaintainList.majorStructure) {
                        edt_maintain_type.setText("主体结构");
                    } else if (patrolFeedbackItemEnum == CommonEnum.MaintainList.safeLock) {
                        edt_maintain_type.setText("安全扣锁");
                    } else if (patrolFeedbackItemEnum == CommonEnum.MaintainList.liftSystem) {
                        edt_maintain_type.setText("升降系统");
                    } else if (patrolFeedbackItemEnum == CommonEnum.MaintainList.pulley) {
                        edt_maintain_type.setText("钢索滑轮");
                    } else if (patrolFeedbackItemEnum == CommonEnum.MaintainList.outline) {
                        edt_maintain_type.setText("外观");
                    } else if (patrolFeedbackItemEnum == CommonEnum.MaintainList.picture) {
                        edt_maintain_type.setText("画面");
                    }
                }

                CommonEnum.LIFTSYSTEM lifEnum = maintainForPatrolBean.getPatrolFeedbackCategoryEnum();
                if (null != lifEnum) {
                    if (lifEnum == CommonEnum.LIFTSYSTEM.NORMAL) {
                        edt_maintain_content.setText("正常");
                    } else if (lifEnum == CommonEnum.LIFTSYSTEM.SHIELD) {
                        edt_maintain_content.setText("遮蔽");
                    } else if (lifEnum == CommonEnum.LIFTSYSTEM.LEAK) {
                        edt_maintain_content.setText("漏水");
                    } else if (lifEnum == CommonEnum.LIFTSYSTEM.RUST) {
                        edt_maintain_content.setText("生锈");
                    } else if (lifEnum == CommonEnum.LIFTSYSTEM.VARIANT) {
                        edt_maintain_content.setText("变形");
                    } else if (lifEnum == CommonEnum.LIFTSYSTEM.LOOSE) {
                        edt_maintain_content.setText("松动");
                    } else if (lifEnum == CommonEnum.LIFTSYSTEM.MALFUNCTION) {
                        edt_maintain_content.setText("故障");

                    } else if (lifEnum == CommonEnum.LIFTSYSTEM.WEAR) {
                        edt_maintain_content.setText("磨损");
                    } else if (lifEnum == CommonEnum.LIFTSYSTEM.NOT_CLEAN) {
                        edt_maintain_content.setText("不洁");
                    } else if (lifEnum == CommonEnum.LIFTSYSTEM.PLICATED) {
                        edt_maintain_content.setText("折皱");
                    } else if (lifEnum == CommonEnum.LIFTSYSTEM.TEAR) {
                        edt_maintain_content.setText("撕裂");
                    } else if (lifEnum == CommonEnum.LIFTSYSTEM.SCRATCH) {
                        edt_maintain_content.setText("划伤");
                    }
                }


            }

        }


    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        edt_engineering_name = findViewById(R.id.edt_engineering_name);
        edt_number = findViewById(R.id.edt_number);
        edt_maintain_type = findViewById(R.id.edt_maintain_type);
        edt_maintain_content = findViewById(R.id.edt_maintain_content);
        rl_handle_date = findViewById(R.id.rl_handle_date);
        edt_handle_date = findViewById(R.id.edt_handle_date);
        btn_submit = findViewById(R.id.btn_submit);

        tv_title.setText("问题处理");

        rl_back.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
    }

    private void btnClickable(boolean bool) {
        isClick = bool;
        if (bool) {
            btn_submit.setBackgroundResource(R.mipmap.btn_long_bg_green);
            btn_submit.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
        } else {

            btn_submit.setBackgroundResource(R.mipmap.btn_bg_short_gray);
            btn_submit.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
        }
    }

    private void handle() {
        patrolMaintainHandlePresenter.patrolMaintainHandle();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.btn_submit:
                if (isClick)
                    handle();
                break;
        }
    }

    @Override
    public String gethandleId() {
        return handleId;
    }

    @Override
    public String getItemId() {
        return itemId;
    }

    @Override
    public String getOrderId() {
        return orderId;
    }

    @Override
    public void patrolMaintainHandleSuccessed() {
        Toast.makeText(this, "处理成功！", Toast.LENGTH_SHORT).show();
        Intent mIntent = new Intent();
        setResult(RESULT_OK, mIntent);
        finish();
    }

    @Override
    public void patrolMaintainHandleFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
